import { createSlice } from '@reduxjs/toolkit'

// 状态类型
const initialState = {
  name: '',
  isLoggedIn: false,
}

/**
 * User 模块的 Redux slice
 */
const userSlice = createSlice({
  name: 'user',
  initialState,
  reducers: {
    login: (state, action) => {
      state.name = action.payload
      state.isLoggedIn = true
    },
    logout: (state) => {
      state.name = ''
      state.isLoggedIn = false
    },
  },
})

// 导出 actions 和 reducer
export const { login, logout } = userSlice.actions
export default userSlice.reducer
